﻿using B.MES.Customer.Application.Application;
using B.MES.Customer.Application.IApplication;
using B.MES.Customer.Domain;
using B.MES.Customer.Domain.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml.Style;
using OfficeOpenXml;

namespace B.MES.Customer.Read.WebApi.Controllers
{
    /// <summary>
    /// 出库管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OutWareHouseController : ControllerBase
    {
        /// <summary>
        /// 出库应用层服务
        /// </summary>
        private readonly IOutboundApplication _outboundApplication;
        /// <summary>
        /// 生产应用层服务
        /// </summary>
        private readonly IProduceApplication    _produceApplication;
        /// <summary>
        /// NLog日志
        /// </summary>
        private readonly ILogger<OutWareHouseController> _logger;
        /// <summary>
        /// 注入构造函数
        /// </summary>
        /// <param name="outboundApplication">出库应用层服务</param>
        public OutWareHouseController(IOutboundApplication outboundApplication, ILogger<OutWareHouseController> logger, IProduceApplication produceApplication)
        {
            _outboundApplication = outboundApplication;
            _logger = logger;
            _produceApplication = produceApplication;
        }
        /// <summary>
        /// 显示、分页、查询出库列表
        /// </summary>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="outTopic">出库主题</param>
        /// <returns> 显示、分页、查询出库列表</returns>
        [HttpGet]
        public IActionResult GetOutWare(int pageSize, int pageIndex, string? outTopic)
        {
            try
            {
                return Ok(_outboundApplication.GetOutWare(pageSize, pageIndex, outTopic));
            }
            catch (Exception ex)
            {
                _logger.LogError("显示、分页、查询出库列表出错" + ex.Message);
                throw;
            }
           
        }
        /// <summary>
        /// 查看出库详情
        /// </summary>
        /// <param name="outId">出库编号</param>
        /// <returns>出库详情</returns>
        [HttpGet]
        public IActionResult GetOutDetail(int outId)
        {
            try
            {
                return Ok(_outboundApplication.GetOutDetail(outId));
            }
            catch (Exception ex)
            {
                _logger.LogError("查看出库详情出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加出库信息并更改生产单状态
        /// </summary>
        /// <param name="outTab">出库表</param>
        /// <param name="proCode"> 生产单编号</param>
        /// <returns> 受影响行数</returns>
        [HttpPost]
        public IActionResult AddOutTab(OutTabDto outTab, string proCode)
        {
            //判断出库数量是否小于等于0
            if (outTab.OutCount <= 0)
            {
                return BadRequest("出库数量不能小于等于0");
            }
            return Ok(_outboundApplication.AddOutTab(outTab , proCode));
        }
        /// <summary>
        /// 获取仓库树形图
        /// </summary>
        /// <param name="pid">父级ID</param>
        /// <returns>仓库树形图</returns>
        [HttpGet]
        public IActionResult GetWarehousesList(int pid=0)
        {
            return Ok(_outboundApplication.GetWarehousesList(pid));
        }
        /// <summary>
        /// 编辑出库信息
        /// </summary>
        /// <param name="outId">出库表主键</param>
        /// <returns>返回出库实体</returns>
        [HttpGet]
        public IActionResult GetOutTabById(int outId)
        {
            return Ok(_outboundApplication.GetOutTabById(outId));
        }
        /// <summary>
        /// 修改出库信息
        /// </summary>
        /// <param name="outTab">要添加的 出库信息对象</param>
        /// <returns> 受影响行数</returns>
        [HttpPut]
        public IActionResult UpdOutInfoById(OutTab outTab)
        {
            return Ok(_outboundApplication.UpdOutInfoById(outTab));
        }
        /// <summary>
        /// 删除出库信息
        /// </summary>
        /// <param name="outId">出库主键</param>
        /// <returns>受影响行数</returns>
        [HttpDelete]
        public IActionResult DelOutById(int outId)
        {
            return Ok(_outboundApplication.DelOutById(outId));
        }
        /// <summary>
        /// 导出仓库列表
        /// </summary>
        /// <returns>仓库列表</returns>
        [HttpGet]
        public IActionResult ExportTab()
        {
            var list = _outboundApplication.GetOutWareExport();
            var ms = new MemoryStream();
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using (var package = new ExcelPackage())
            {
                var worksheet = package.Workbook.Worksheets.Add("Sheet1");

                worksheet.Workbook.Properties.SetCustomPropertyValue("Lauguage", "zh-CN");

                // 设置表头样式
                using (var range = worksheet.Cells["A1:H1"])
                {
                    range.Style.Font.Bold = true;
                    range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                }
                worksheet.Cells["A1"].Value = "序号";
                worksheet.Cells["B1"].Value = "出库类型";

                worksheet.Cells["C1"].Value = "出库单号";
                worksheet.Cells["D1"].Value = "出库主题";
                worksheet.Cells["E1"].Value = "出库日期";
                worksheet.Cells["F1"].Value = "出库仓库";
                worksheet.Cells["G1"].Value = "申领人员";
                worksheet.Cells["H1"].Value = "出库人员";

                for (int i = 0; i < list.Count; i++)
                {
                    worksheet.Cells["A" + (i + 2)].Value = i + 1;
                    worksheet.Cells["B" + (i + 2)].Value = list[i].OutType;

                    worksheet.Cells["C" + (i + 2)].Value = list[i].OutNum;
                    worksheet.Cells["D" + (i + 2)].Value = list[i].OutTopic;
                    worksheet.Cells["E" + (i + 2)].Value = list[i].OutDate;
                    worksheet.Cells["F" + (i + 2)].Value = list[i].OutWareHouse;
                    worksheet.Cells["G" + (i + 2)].Value = list[i].Applicant;
                    worksheet.Cells["H" + (i + 2)].Value = list[i].OutName;
                }

                package.SaveAs(ms);
            }
            ms.Seek(0, SeekOrigin.Begin);

            return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "出库信息表单.xlsx"
            };
        }
        ///// <summary>
        ///// 获取生产列表
        ///// </summary>
        ///// <param name="ProCode"> 生产单号</param>
        ///// <param name="pageIndex"> 页码</param>
        ///// <param name="pageSize"> 页大小</param>
        ///// <returns> 生产列表</returns>
        //[HttpGet]
        //public IActionResult GetProduceShow(string? ProCode, int pageIndex, int pageSize)
        //{
        //    return Ok(_produceApplication.GetProduceShow(ProCode, pageIndex, pageSize));
        //}
        /// <summary>
        /// 获取生产列表
        /// </summary>
        /// <param name="ProCode"> 生产单号</param>
        /// <param name="pageIndex"> 页码</param>
        /// <param name="pageSize"> 页大小</param>
        /// <returns> 生产列表</returns>
        [HttpGet]
        public IActionResult GetProduceShows(string? ProCode, int pageIndex, int pageSize)
        {
            return Ok(_outboundApplication.GetProduceShow(ProCode, pageIndex, pageSize));
        }
    }
}
